'''
Created on Sept 16, 2013

@author: Patrick Janssen
'''

import sys, os
from string import Template
from core.modules.vistrails_module import Module, ModuleError
import json
from constants import *

''' ---------------------------------------------------------------------------
Parameters
--------------------------------------------------------------------------- '''

class Parameters(Module):
    def compute(self):

        #Get the input parameters
        params = {}
        if self.hasInputFromPort("integers"):
        	list_param_data = self.forceGetInputListFromPort("integers") #gets a list
        	for param_data in list_param_data:
	        	param_key = param_data[0]
	        	param_value = param_data[1]
	        	params[param_key] = param_value
        if self.hasInputFromPort("floats"):
        	list_param_data = self.forceGetInputListFromPort("floats") #gets a list
        	for param_data in list_param_data:
	        	param_key = param_data[0]
	        	param_value = param_data[1]
	        	params[param_key] = param_value
        if self.hasInputFromPort("strings"):
        	list_param_data = self.forceGetInputListFromPort("strings") #gets a list
        	for param_data in list_param_data:
	        	param_key = param_data[0]
	        	param_value = param_data[1]
	        	params[param_key] = param_value

        #Create the file
        json_output_file = self.interpreter.filePool.create_file(suffix=".json")
        f = open(json_output_file.name, 'w')
        f.write(json.dumps(params))
        f.close()

        #Set output
        self.setResult("json_output_file", json_output_file)

    _input_ports = [
    	("integers",STRING_INTEGER,{"optional":True,"sort_key":1}),
		("floats",STRING_FLOAT,{"optional":True,"sort_key":2}),
		("strings",STRING_STRING,{"optional":True,"sort_key":3})
    ]

    _output_ports = [
        ("json_output_file",FILE,{"optional":False,"sort_key":1})
    ]

''' ---------------------------------------------------------------------------
ParametersX
--------------------------------------------------------------------------- '''

class ParametersX(Module):
    def compute(self):

        #Get the input parameters
        params = {}

        for i in range(1,4,1):
        	key = "key_" + str(i)
        	value = "value_" + str(i)
	        if self.hasInputFromPort(key) and self.hasInputFromPort(value):
	        	param_key = self.getInputFromPort(key)
	        	param_value = self.getInputFromPort(value)
	        	params[param_key] = param_value


        #Create the file
        json_output_file = self.interpreter.filePool.create_file(suffix=".json")
        f = open(json_output_file.name, 'w')
        f.write(json.dumps(params))
        f.close()

        #Set output
        self.setResult("json_output_file", json_output_file)

    _input_ports = [
    	("key_1",STRING,{"optional":True,"sort_key":1}),
		("value_1",FLOAT,{"optional":True,"sort_key":2}),

    	("key_2",STRING,{"optional":True,"sort_key":3}),
		("value_2",FLOAT,{"optional":True,"sort_key":4}),

    	("key_3",STRING,{"optional":True,"sort_key":5}),
		("value_3",FLOAT,{"optional":True,"sort_key":6})
    ]

    _output_ports = [
        ("json_output_file",FILE,{"optional":False,"sort_key":1})
    ]

''' ---------------------------------------------------------------------------
ParametersD
--------------------------------------------------------------------------- '''

class ParametersDict(Module):
    def compute(self):

        #Get the input parameters
        params = {}

        for i in range(1,4,1):
        	param_dict_name = "dict_" + str(i)
	        if self.hasInputFromPort(param_dict_name):
	        	param_dict = self.getInputFromPort(param_dict_name)
	        	for key, value in param_dict.iteritems():
		        	params[key] = value

        #Create the file
        json_output_file = self.interpreter.filePool.create_file(suffix=".json")
        f = open(json_output_file.name, 'w')
        f.write(json.dumps(params))
        f.close()

        #Set output
        self.setResult("json_output_file", json_output_file)
        self.setResult("json_output_file_path", json_output_file.name)

    _input_ports = [
    	("dict_1",DICTIONARY,{"optional":True,"sort_key":1}),
    	("dict_2",DICTIONARY,{"optional":True,"sort_key":2}),
    	("dict_3",DICTIONARY,{"optional":True,"sort_key":3}),
    ]

    _output_ports = [
        ("json_output_file",FILE,{"optional":False,"sort_key":1})
    ]